package filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebFilter(filterName = "EncodingFilter",urlPatterns = "*")
public class EncodingFilter implements Filter {

    public EncodingFilter(){
        System.out.println("我是字符编码过滤器");
    }

    public void init(FilterConfig config) throws ServletException {
        System.out.println("我是字符编码过滤器的初始化方法");
    }

    public void destroy() {
        System.out.println("我是字符编码过滤器的销毁方法");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        System.out.println("设置字符编码为utf-8");

        chain.doFilter(request, response);

        System.out.println("字符编码过滤器response过滤");
    }
}
